Skip to content

chore: 🚙 type some helper files and constants #860

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Dec 1, 2021

Conversation

tiffafoo
Copy link

Summary

Our Netlify Next Plugin is written in mostly JS, but we'd like it to be using TypeScript for readability and prevent some bugs which we've had that could have been avoided with TS. This PR changes a couple files to use TypeScript, as I go through the code and get more familiar with it.

Point of entries

src/helpers/index.js is one of the bigger files, and uses many of the other helpers. Thus I decided to go through the rest of the helpers to minimize impact.

The files touched by this conversion:

  • src/constants.js
  • src/helpers/cache.js
  • src/helpers/config.js

Test plan

  1. Visit the Deploy Preview, see that everything works as expected

Relevant links (GitHub issues, Notion docs, etc.) or a picture of cute animal

Standard checks:

  • Check the Deploy Preview's Demo site for your PR's functionality
  • Add docs when necessary

🧪 Once merged, make sure to update the version if needed and that it was published correctly.

@netlify
Copy link

netlify bot commented Nov 30, 2021

✔️ Deploy Preview for netlify-plugin-nextjs-nx-monorepo-demo canceled.

🔨 Explore the source changes: 2c32256

🔍 Inspect the deploy log: https://app.netlify.com/sites/netlify-plugin-nextjs-nx-monorepo-demo/deploys/61a74200020e9200077bfc6d

@github-actions github-actions bot added the type: chore work needed to keep the product and development running smoothly label Nov 30, 2021
@@ -123,6 +131,8 @@ exports.generateRedirects = async ({ netlifyConfig, basePath, i18n }) => {
from: `${basePath}/*`,
to: HANDLER_FUNCTION_PATH,
status: 200,
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I left that in as I did the first pass to TS. I figured we'd want to get this in incrementally and worry too much about problematic missing types for now.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this type missing in @netlify/build then?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nah just one that would require a bit of refactoring of the function this ts-ignore refers to

if (!config) {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here, expects a second object and I just ignored it as I passed through :P

return failBuild('Error loading your Next config')
}
return { ...config, appDir, ignore }
} catch (error) {
} catch (error: unknown) {
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is best practice to have errors as unknown since there's no good way to predict the type of an error

ascorbic
ascorbic previously approved these changes Nov 30, 2021
Copy link
Contributor

@ascorbic ascorbic left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fantastic! Thanks so much for doign this.

import { NetlifyConfig } from '@netlify/build'
import { yellowBright } from 'chalk'
import { readJSON } from 'fs-extra'
import { PrerenderManifest } from 'next/dist/build'
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can this be import type?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can't do import { PrerenderManifest } from 'next/types' since they don't have it in their types, is that what you mean?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, I just mean use the type import syntax to make it clear that you're only importing the type:

import type { PrerenderManifest } from 'next/dist/build'

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ohhh TIL

export const generateRedirects = async ({ netlifyConfig, basePath, i18n }: {
netlifyConfig: NetlifyConfig,
basePath: string,
i18n
Copy link
Contributor

@ascorbic ascorbic Nov 30, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can get this type as NextConfig["i18n"] from next

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oo thanks! Wasn't sure where it was coming from

@@ -123,6 +131,8 @@ exports.generateRedirects = async ({ netlifyConfig, basePath, i18n }) => {
from: `${basePath}/*`,
to: HANDLER_FUNCTION_PATH,
status: 200,
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this type missing in @netlify/build then?

@@ -180,7 +192,7 @@ const resolveModuleRoot = (moduleName) => {

const DEFAULT_EXCLUDED_MODULES = ['sharp', 'electron']

exports.configureHandlerFunctions = ({ netlifyConfig, publish, ignore = [] }) => {
export const configureHandlerFunctions = ({ netlifyConfig, publish, ignore = [] }) => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can these args be typed?

@@ -0,0 +1,129 @@
/* eslint-disable eslint-comments/disable-enable-pair, @typescript-eslint/no-empty-interface, no-use-before-define */
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, nice. Maybe we should contribute this upstream!

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good call! Although I'm sure the definition file is missing some types since it was just auto-generated based on the demo's required-server-files.json

} = require('path')
import { posix } from 'path'

export const restoreCache = async ({ cache, publish }) => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I htink it's a good idea if we ensure that all exported functions have typed args and return values, even if they're just defined inline.

"fr/static.html",
]
`;
exports[`onBuild() generates static files manifest 1`] = `Array []`;
Copy link
Contributor

@ascorbic ascorbic Nov 30, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You updated the snapshot after running ntl build, which means the files had already been moved. This is why I added the updateSnapshot npm script: you need to just build the site, not run the plugin too.

Copy link
Contributor

@ascorbic ascorbic left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

snapidoo!

@kodiakhq kodiakhq bot merged commit d49f56a into main Dec 1, 2021
@kodiakhq kodiakhq bot deleted the tln/type-all-the-things-1 branch December 1, 2021 09:39
export interface Config {
env?: Env;
webpack?: null;
webpackDevMiddleware?: null;
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

^ these will have to be manually updated, since it's clearly wrong

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
automerge type: chore work needed to keep the product and development running smoothly
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants